import com.lagou.dao.IUserDao;
import com.lagou.io.Resources;
import com.lagou.sqlSession.SqlSession;
import com.lagou.sqlSession.SqlSessionFactory;
import com.lagou.sqlSession.SqlSessionFactoryBuilder;
import org.junit.Test;
import com.lagou.pojo.User;

import java.io.InputStream;
import java.util.List;

//123
public class IPersistenceTest {


    //测试
    @Test
    public void testSelectAll() throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

//        User userResult = sqlSession.selectOne("user.selectOne",user);
//        System.out.println("selectOne查询：id:"+userResult.getId()+",userName:"+userResult.getUsername());
//
//        List<User> list = sqlSession.selectList("user.selectList",null);
//        if(list != null && list.size()>0){
//            list.stream().forEach(u->{
//                System.out.println("selectList查询：id:"+u.getId()+",userName:"+u.getUsername());
//            });
//        }

        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        List<User> list = userDao.findAll();
        if(list != null && list.size()>0){
            list.stream().forEach(u->{
                System.out.println("findAll查询：id:"+u.getId()+",userName:"+u.getUsername());
            });
        }
    }

    @Test
    public void testFindByCondition() throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        User user = new User();
        user.setId(1);
        user.setUsername("李雷");
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        User user2 = userDao.findByCondition(user);
        System.out.println("findByCondition查询：id:"+user2);
    }

    @Test
    public void testInsert() throws Exception{
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        User user = new User();
        user.setId(4);
        user.setUsername("韩梅-123");
        int rows = userDao.insert(user);
        System.out.println("插入的条数："+rows);
    }

    @Test
    public void testUpdate() throws Exception{
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        User user = new User();
        user.setId(4);
        user.setUsername("韩梅-456");
        int rows = userDao.update(user);
        System.out.println("更新的条数："+rows);

    }

    @Test
    public void testDelete() throws Exception{
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        User user = new User();
        user.setId(4);
        int rows = userDao.delete(user);
        System.out.println("删除的条数："+rows);
    }


}
